import { CHANGE_SUNBATHINGLIST, CHANGE_SUNBATH_SEL_LIST, CHANGE_SUNBATH_SELECTED } from "../mutation-type"
import shuijiao from "@/pages_me/static/img/shuijiao.jpg"

const sunbathingBar = {
    namespaced: true,
    state: () => ({
        sunbathingList: [], // 晒单列表
        sunbathSelList: [], // 选择晒单商品列表
        selectedSunBathProducts: [], // 选中的晒单商品
    }),
    mutations: {
        [CHANGE_SUNBATHINGLIST](state, list) {
            if (list.length == 0) {
                state.sunbathingList = []
            } else {
                state.sunbathingList.push(...list)
            }
        },
        [CHANGE_SUNBATH_SEL_LIST](state, list) {
            if (list.length == 0) {
                state.sunbathSelList = []
            } else {
                state.sunbathSelList.push(...list)
            }
        },
        [CHANGE_SUNBATH_SELECTED](state, payload) {
			if(!payload.ID){
				state.selectedSunBathProducts = []
				return
			}
            const index = state.selectedSunBathProducts.findIndex((item) => item.ID == payload.ID)
			console.log(index)
            if (index == -1) {
                state.selectedSunBathProducts.push(payload)
            } else {
				state.selectedSunBathProducts.forEach((pro,i) => {
					if(pro.ID == payload.ID){
						state.selectedSunBathProducts.splice(i, 1)
					}
				})
				state.selectedSunBathProducts.push(payload)
                if (payload.count == 0) {
                    state.selectedSunBathProducts.splice(index, 1)
                }
            }
        },
    },
    getters: {
        getTotalReward(state) {
			let total = {
			    totalIntegral: 0,
			    totalPrice: 0
			}
			state.selectedSunBathProducts.forEach((item)=>{
				if(item.RewardType==1){
					total.totalIntegral += item.Reward * item.count
				}else{
					total.totalPrice += item.Reward * item.count
				}
			})
			total.totalIntegral = total.totalIntegral.toFixed(2)
			total.totalPrice = total.totalPrice.toFixed(2)
            return total
        },
    },
    actions: {
        fetchSunbathingList({ commit }) {
            const data = []
            commit(CHANGE_SUNBATHINGLIST, data)
        },
        fetchSunbathSelList({ commit }) {
            const data = []
            commit(CHANGE_SUNBATH_SEL_LIST, data)
        },
    },
}

export default sunbathingBar
